package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.bean.Permission;
import com.mall.bean.PermissionExample;
import com.mall.dao.PermissionMapper;
import com.mall.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-15 17 54
 * discription
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int insert(Permission permission) {
        return 0;
    }

    @Override
    public List<Permission> queryData(Permission permission) {
        return null;
    }

    @Override
    public int update(Permission permission) {
        return 0;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public Permission queryDataById(String id) {
        return null;
    }

    @Override
    public PageInfo<Permission> queryDataByPage(int currentPage, int pageSize, Permission permission) {
        PageHelper.startPage(currentPage, pageSize);

        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(1);
        if (permission.getName() != null) {
            criteria.andNameLike("%"+permission.getName()+"%");
        }
        List<Permission> permissionList = permissionMapper.selectByExample(example);
        PageInfo<Permission> permissionPageInfo = new PageInfo<>(permissionList);
        return permissionPageInfo;
    }

    @Override
    public List<Permission> getPermissionListByRoleId(String roleId) {
        List<Permission> permissionList = permissionMapper.getAllPermissionByRoleId(Integer.parseInt(roleId));
//        List<Permission> permissionList =  permissionMapper.getPermissionListByRoleId(roleId);
//        //获得所有权限 然来来组装树结构
//        HashMap<Integer, Permission> map = new HashMap<>();
//        //组装permissionList的层次结构
//        for (Permission permission : permissionList) {
//            //找父节点
//            if (permission.getParent_id() == 0) {
//                map.put(permission.getPermission_id(), permission);
//            }
//        }
//        for (Permission permission : permissionList) {
//            if (permission.getParent_id() == 0) {
//                continue;
//            }
//            Permission permissionParent = map.get(permission.getParent_id());
//            if (permissionParent != null) {
//                List<Permission> children = permissionParent.getChildren();
//                if (children == null) {
//                    children = new ArrayList<>();
//                    permissionParent.setChildren(children);
//                    children.add(permission);
//                } else {
//                    children.add(permission);
//                }
//            }
//        }
//        permissionList.clear();
//
//        permissionList.addAll(map.values());

        return permissionList;
    }

    @Override
    public List<Permission> getPermissionList() {
        List<Permission> list = permissionMapper.getAllPermissionCeil();
        return list;
    }

    @Override
    public int deletePermissionById(Integer permission_id) {
//        int res = permissionMapper.deleteByPrimaryKey(permission_id);
        Permission permission = new Permission();
        permission.setStatus(0);
        permission.setPermission_id(permission_id);
        int res = permissionMapper.updateByPrimaryKeySelective(permission);

        return res;

    }

    @Override
    public int updatePermissionById(Permission permission) {
        int res = permissionMapper.updateByPrimaryKeySelective(permission);
        return res;
    }
}
